import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'dashboard',
        meta: { title: '系统首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: 'customer',
    component: Layout,
    redirect: '/customer',
    children: [
      {
        path: '/customer',
        component: () => import('@/views/customer/index'),
        name: 'customer',
        meta: { title: '客户管理', icon: 'el-icon-s-custom', affix: true }
      },
      {
        path: '/customer/add',
        component: () => import('@/views/customer/detail'),
        name: 'customerAdd',
        hidden: true,
        meta: { title: '新增客户', activeMenu: '/customer' }
      },
      {
        path: '/customer/edit/:id',
        component: () => import('@/views/customer/detail'),
        hidden: true,
        name: 'customerEdit',
        meta: { title: '客户详情', activeMenu: '/customer' }
      }
    ]
  },
  {
    path: 'provider',
    component: Layout,
    redirect: '/provider',
    children: [
      {
        path: '/provider',
        component: () => import('@/views/provider/index'),
        name: 'provider',
        meta: { title: '供应商管理', icon: 'el-icon-sell', affix: true }
      },
      {
        path: '/provider/add',
        component: () => import('@/views/provider/detail'),
        name: 'providerAdd',
        hidden: true,
        meta: { title: '新增供应商', activeMenu: '/provider' }
      },
      {
        path: '/provider/edit/:id',
        component: () => import('@/views/provider/detail'),
        hidden: true,
        name: 'providerEdit',
        meta: { title: '供应商详情', activeMenu: '/provider' }
      }
    ]
  },
  {
    path: 'project',
    component: Layout,
    redirect: '/project',
    children: [
      {
        path: '/project',
        component: () => import('@/views/project/index'),
        name: 'project',
        meta: { title: '项目管理', icon: 'el-icon-takeaway-box', affix: true }
      },
      {
        path: '/project/add',
        component: () => import('@/views/project/detail'),
        name: 'projectAdd',
        hidden: true,
        meta: { title: '新增项目', activeMenu: '/project' }
      },
      {
        path: '/project/edit/:id',
        component: () => import('@/views/project/detail'),
        hidden: true,
        name: 'projectEdit',
        meta: { title: '项目详情', activeMenu: '/project' }
      }
    ]
  },
  {
    path: 'bids',
    component: Layout,
    redirect: '/bids',
    name: 'bids',
    meta: {
      title: '投标管理',
      icon: 'el-icon-s-flag'
    },
    children: [
      {
        path: '/cert',
        name: 'cert',
        component: () => import('@/views/cert/index'),
        meta: { title: '公司资质' }
      },
      {
        path: '/cert/add',
        component: () => import('@/views/cert/detail'),
        name: 'certAdd',
        hidden: true,
        meta: { title: '新增公司资质', activeMenu: '/cert' }
      },
      {
        path: '/cert/edit/:id',
        component: () => import('@/views/cert/detail'),
        hidden: true,
        name: 'certEdit',
        meta: { title: '公司资质详情', activeMenu: '/cert' }
      },
      {
        path: '/bid',
        name: 'bid',
        component: () => import('@/views/bid/index'),
        meta: { title: '招投文件' }
      },
      {
        path: '/bid/add',
        component: () => import('@/views/bid/detail'),
        name: 'fileAdd',
        hidden: true,
        meta: { title: '新增招投文件', activeMenu: '/bid' }
      },
      {
        path: '/bid/edit/:id',
        component: () => import('@/views/bid/detail'),
        hidden: true,
        name: 'fileEdit',
        meta: { title: '招投文件详情', activeMenu: '/bid' }
      },
      {
        path: '/winning',
        name: 'winning',
        component: () => import('@/views/winning/index'),
        meta: { title: '中标通知' }
      },
      {
        path: '/winning/add',
        component: () => import('@/views/winning/detail'),
        name: 'winningAdd',
        hidden: true,
        meta: { title: '新增中标通知', activeMenu: '/winning' }
      },
      {
        path: '/winning/edit/:id',
        component: () => import('@/views/winning/detail'),
        hidden: true,
        name: 'winningEdit',
        meta: { title: '中标通知详情', activeMenu: '/winning' }
      }
    ]
  },
  {
    path: 'contracts',
    component: Layout,
    redirect: '/contracts',
    name: 'contracts',
    meta: {
      title: '合同管理',
      icon: 'el-icon-tickets'
    },
    children: [
      {
        path: '/contract',
        name: 'contract',
        component: () => import('@/views/contract'),
        meta: { title: '收入合同' }
      },
      {
        path: '/contract/add',
        component: () => import('@/views/contract/detail'),
        name: 'contractAdd',
        hidden: true,
        meta: { title: '新增收入合同', activeMenu: '/contract' }
      },
      {
        path: '/contract/edit/:id',
        component: () => import('@/views/contract/detail'),
        hidden: true,
        name: 'contractEdit',
        meta: { title: '收入合同详情', activeMenu: '/contract' }
      },
      {
        path: '/purchase',
        name: 'purchase',
        component: () => import('@/views/purchase'),
        meta: { title: '采购合同' }
      },
      {
        path: '/purchase/add',
        component: () => import('@/views/purchase/detail'),
        name: 'purchaseAdd',
        hidden: true,
        meta: { title: '新增采购合同', activeMenu: '/purchase' }
      },
      {
        path: '/purchase/edit/:id',
        component: () => import('@/views/purchase/detail'),
        hidden: true,
        name: 'purchaseEdit',
        meta: { title: '采购合同详情', activeMenu: '/purchase' }
      }
    ]
  },
  {
    path: 'assets',
    component: Layout,
    redirect: '/reimbursement',
    name: 'assets',
    meta: {
      title: '财务管理',
      icon: 'el-icon-s-data'
    },
    children: [
      {
        path: '/reimbursement',
        name: 'reimbursement',
        component: () => import('@/views/reimbursement/index'),
        meta: { title: '报销申请' }
      },
      {
        path: '/reimbursement/add',
        component: () => import('@/views/reimbursement/detail'),
        name: 'reimbursementAdd',
        hidden: true,
        meta: { title: '新增报销', activeMenu: '/reimbursement' }
      },
      {
        path: '/reimbursement/edit/:id',
        component: () => import('@/views/reimbursement/detail'),
        hidden: true,
        name: 'reimbursementEdit',
        meta: { title: '报销详情', activeMenu: '/reimbursement' }
      },
      {
        path: '/pay',
        name: 'pay',
        component: () => import('@/views/pay/index'),
        meta: { title: '付款申请' }
      },
      {
        path: '/pay/add',
        component: () => import('@/views/pay/detail'),
        name: 'payAdd',
        hidden: true,
        meta: { title: '新增支付申请', activeMenu: '/pay' }
      },
      {
        path: '/pay/edit/:id',
        component: () => import('@/views/pay/detail'),
        hidden: true,
        name: 'payEdit',
        meta: { title: '支付申请详情', activeMenu: '/pay' }
      },
      {
        path: '/invoice',
        name: 'invoice',
        component: () => import('@/views/invoice/index'),
        meta: { title: '发票申请' }
      },
      {
        path: '/invoice/add',
        component: () => import('@/views/invoice/detail'),
        name: 'invoiceAdd',
        hidden: true,
        meta: { title: '新增发票申请', activeMenu: '/invoice' }
      },
      {
        path: '/invoice/edit/:id',
        component: () => import('@/views/invoice/detail'),
        hidden: true,
        name: 'invoiceEdit',
        meta: { title: '发票申请详情', activeMenu: '/invoice' }
      },
      {
        path: '/returned',
        name: 'returned',
        component: () => import('@/views/returned/index'),
        meta: { title: '回款管理', activeMenu: '/returned' }
      },
      {
        path: '/returned/add',
        component: () => import('@/views/returned/detail'),
        name: 'returnedAdd',
        hidden: true,
        meta: { title: '新增回款', activeMenu: '/returned' }
      },
      {
        path: '/returned/edit/:id',
        component: () => import('@/views/returned/detail'),
        hidden: true,
        name: 'returnedEdit',
        meta: { title: '回款详情', activeMenu: '/returned' }
      }
    ]
  },
  {
    path: 'admin',
    component: Layout,
    redirect: '/admin',
    alwaysShow: true,
    hidden: false,
    name: 'admin',
    meta: {
      title: '人员管理',
      icon: 'el-icon-user-solid',
      roles: [666]
    },
    children: [
      {
        path: '/admin',
        component: () => import('@/views/admin/index'),
        name: 'adminIndex',
        meta: { title: '员工管理', roles: [666] }
      },
      {
        path: '/department',
        component: () => import('@/views/department/index'),
        name: 'department',
        meta: { title: '部门管理' }
      },
      {
        path: '/admin/add',
        component: () => import('@/views/admin/detail'),
        name: 'adminAdd',
        hidden: true,
        meta: { title: '新增人员', activeMenu: '/admin' }
      },
      {
        path: '/admin/edit/:id',
        component: () => import('@/views/admin/detail'),
        hidden: true,
        name: 'adminEdit',
        meta: { title: '编辑人员', activeMenu: '/admin' }
      },
      {
        path: '/departmentAdd',
        component: () => import('@/views/department/detail'),
        name: 'departmentAdd',
        hidden: true,
        meta: { title: '新增部门', activeMenu: '/department' }
      },
      {
        path: '/departmentEdit/:id',
        component: () => import('@/views/department/detail'),
        hidden: true,
        name: 'departmentEdit',
        meta: { title: '编辑部门', activeMenu: '/department' }
      }
    ]
  },
  {
    path: 'system',
    component: Layout,
    redirect: '/system',
    alwaysShow: true,
    name: 'system',
    meta: {
      title: '系统管理',
      icon: 'el-icon-set-up',
      roles: [666]
    },
    children: []
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
